<?php

class Admin_indexController extends Controller {

	public function indexAction() {


		$admin_baseController = new Admin_baseController();

		if ($admin_baseController::$admin_info['role_id'] == adminModel::SHOP_ROLE) {
			Response::redirect(Router::getBaseUrl() . '/admin_index/shop');
		}
		$admin_nodel_ids = self::model('admin_access')->getAllByRoleId($admin_baseController::$admin_info['role_id']);
		$admin_baseController->initHeader($admin_baseController::$admin_info, $admin_nodel_ids);
		$admin_baseController->initMenu($admin_baseController::$admin_info, $admin_nodel_ids);

		$todayData = date('Y-m-d 00:00:00');
		$todayTime = strtotime($todayData);
//        $this->assign('totalUser', self::model('user')->count());
		//        $this->assign('todayUser', self::model('user')->count(null, 'add_time>=?', $todayData));
		//
		//        $this->assign('totalDynamic', self::model('dynamic')->count());
		//        $this->assign('todayDynamic', self::model('dynamic')->count(null, 'add_time>=?', $todayData));
		//
		//        $this->assign('totalPayOrder', self::model('order')->count(null, 'pay_status=?', [orderModel::PAY_STATUS_PAIED]));
		//        $this->assign('todayPayOrder', self::model('order')->count(null, 'pay_status=? and add_time>=?', [orderModel::PAY_STATUS_PAIED, $todayData]));
		//
		//        $this->assign('totalFeedback', self::model('feedback')->count());
		//        $this->assign('todayFeedback', self::model('feedback')->count(null, 'add_time>=?', [$todayData]));
		//
		//        $this->assign('totalReport', self::model('report')->count());
		//        $this->assign('todayReport', self::model('report')->count(null, 'add_time>=?', [$todayData]));

		$this->display();
	}

	public function shopLoginAction() {
		if (Request::isGet()) {

			$this->display();
			exit;
		}
		$data = array();
		$mobile = Security::removeXss(Request::post('name'));
		$password = Security::removeXss(Request::post('password'));
		if (!$mobile || !$password) {
			Response::ajax(NORMAL_ERROR, '手机号或密码不能为空');
		}
		if (!$admin_info = self::model('user')->getOne('mobile=?', $mobile)) {
			Response::ajax(NORMAL_ERROR, '手机号不存在');
		}
		if ($admin_info['password'] != md5(md5($password) . $admin_info['salt'])) {
			Response::ajax(NORMAL_ERROR, '账号密码填写有误,登录失败');
		}
		if ($admin_info['status'] == 2) {
			Response::ajax(USER_FORBID, '您的账号被屏蔽,请联系管理员处理');
		}
		if ($admin_info['type'] == 3 || $admin_info['type'] == 4) {

			$admin_info1 = array(
				'admin_id' => $admin_info['uid'],
				'shop_id' => $admin_info['uid'],
				'name' => $admin_info['mobile'],
				'role_id' => '2',
			);
			self::model('admin')->login($admin_info1);
			Response::ajax(200, '登录成功', array(
				'url' => Router::getBaseUrl() . '/admin_index/index',
			));

		} else {
			Response::ajax(NORMAL_ERROR, '抱歉,您填写的手机号或者不是店主');
		}

	}
	public function shopAction() {
		$admin_baseController = new Admin_baseController();

		$this->display();
	}

	public function loginAction() {
		if (Request::isGet()) {

			$this->display();
			exit;
		}
		$data = array();
		$data['name'] = Security::removeXss(Request::post('name'));
		$data['password'] = Security::removeXss(Request::post('password'));
		$result = self::model('admin')->validata($data);
		if ($result !== true) {
			Response::ajax(NORMAL_ERROR, $result);
		}
		if (!$admin_info = self::model('admin')->getOne('name=?', array($data['name']))) {
			Response::ajax(NORMAL_ERROR, '您填写的账号或密码有误');
		}
		if ($admin_info['status'] == 2 || $admin_info['is_delete']) {
			Response::ajax(USER_FORBID, '您的账号已停用,请联系管理员处理');
		}
		if (!$admin_info['role_id'] || !self::model('admin_role')->getOne('role_id=?', $admin_info['role_id'])) {
			Response::ajax(USER_FORBID, '您的账号管理权限已停用,请联系管理员处理');
		}

		if (md5($data['password'] . $admin_info['salt']) == $admin_info['password']) {
			self::model('admin')->login($admin_info);
			unset($admin_info['password'], $admin_info['salt']);
			$admin_info['last_time'] = time();
			self::model('admin')->update(['update_time' => date('Y-m-d H:i:s'), 'login_time' => date('Y-m-d H:i:s')], 'admin_id=?', $admin_info['admin_id']);
			Response::ajax(200, '登录成功', array(
				'url' => Router::getBaseUrl() . '/admin_index/index',
			));
		}
		Response::ajax(NORMAL_ERROR, '您填写的账号或密码有误');
	}

	public function logoutAction() {
		self::model('admin')->logout();
		Response::redirect(Router::getBaseUrl() . '/admin_index/login');
	}

	public function shoplogoutAction() {
		self::model('admin')->logout();
		Response::redirect(Router::getBaseUrl() . '/admin_index/shoplogin');
	}

}
